package com.maozzi.common.model;

import com.maozzi.common.enums.BizCodeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Objects;

/**
 * 返回值基类
 *
 * @author maozi
 */
@Data
@NoArgsConstructor
public class ResultVO<T> implements Serializable {
    private String code;
    private String message;
    private T data;

    private ResultVO(BizCodeEnum codeEnum, T data) {
        this(codeEnum.getCode(), codeEnum.getDescription(), data);
    }

    private ResultVO(String code, String message) {
        this(code, message, null);
    }

    private ResultVO(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ResultVO<T> success(T data) {
        return new ResultVO<>(BizCodeEnum.OK, data);
    }

    public static ResultVO<String> fail(String message) {
        return fail(BizCodeEnum.SERVER_ERROR.getCode(), message);
    }

    public static ResultVO<String> fail(String code, String message) {
        BizCodeEnum codeEnum = BizCodeEnum.SERVER_ERROR;
        if (Objects.isNull(code)) {
            code = codeEnum.getCode();
        }
        if (Objects.isNull(message)) {
            message = codeEnum.getDescription();
        }
        return new ResultVO<>(code, message);
    }
}
